package com.captain.leetcode.二叉树;

import com.captain.leetcode.TreeNode;

import java.util.LinkedList;

/**
 * Des:
 *
 * @author XL
 * @Date 2021/7/28 13:15
 */
public class 二叉树的最大深度104 {

    //BFS求解 效率不好
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int maxDept = 0;
        while (!queue.isEmpty()) {
            maxDept++;
            int n = queue.size();
            for (int i = 0; i < n; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }

        }
        return maxDept;
    }
}
